﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLNSClient
{
    
    
    public partial class uc_dsDMDV : UserControl
    {

        QLNSServiceReference.QLNSServiceClient sv = new QLNSServiceReference.QLNSServiceClient();

        public void LoadData()
        {
            btn_Luu.Enabled = false;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            btn_Huy.Enabled = false;

            txt_maDMDV.Enabled = false;
            txt_tenDMDV.Enabled = false;
            lke_dsDVCQ.Enabled = false;
            lke_loaiDV.Enabled = false;

            lke_dsDVCQ.Properties.DataSource = sv.getDVHC();
            lke_loaiDV.Properties.DataSource = sv.getLoaiDV();

            gv_dsDMDV.DataSource = sv.getDMDV();
            gridView1.Columns[0].Caption = "Mã đơn vị";
            gridView1.Columns[1].Caption = "Tên đơn vị";
            gridView1.Columns[2].Caption = "Đơn vị chủ quản";
            gridView1.Columns[3].Caption = "Loại đơn vị";

            if (gridView1.DataRowCount > 0)
            {
                txt_maDMDV.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[0]);
                txt_tenDMDV.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[1]);
                lke_dsDVCQ.EditValue = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[2]);
                lke_loaiDV.EditValue = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[3]);
            }
            else
            {
                MessageBox.Show("Chưa có dữ liệu!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            for (int i = 0; i < 4; i++)
            {
                gridView1.Columns[i].OptionsColumn.AllowEdit = false;
            }
        }

        public uc_dsDMDV()
        {
            InitializeComponent();
        }

        private void uc_dsDMDV_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            btn_Huy.Enabled = true;

            lke_dsDVCQ.EditValue = "";
            lke_loaiDV.EditValue = "";
            txt_tenDMDV.Text = "";

            txt_tenDMDV.Enabled = true;
            lke_dsDVCQ.Enabled = true;
            lke_loaiDV.Enabled = true;

            List<QLNSServiceReference.DANHMUC_DONVI> listLastID = new List<QLNSServiceReference.DANHMUC_DONVI>(sv.getLastDVTT());
            string a = listLastID[0].Ma_DV;
            int vt = a.IndexOf("V");
            string lastnum = a.Substring(vt + 1, 3);
            int tempID = Int32.Parse(lastnum);
            int newnum = tempID + 1;

            string newid;
            if (newnum < 10)
            {
                newid = "DV00" + newnum.ToString();
                txt_maDMDV.Text = newid;
            }
            if (newnum > 10 && newnum < 100)
            {
                newid = "DV0" + newnum.ToString();
                txt_maDMDV.Text = newid;
            }
            else
            {
                newid = "DV" + newnum.ToString();
                txt_maDMDV.Text = newid;
            }
            
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            string tempMaDMDV = txt_maDMDV.Text;
            string temptenDMDV = txt_tenDMDV.Text;
            string tempDVCQ="";
            if (lke_dsDVCQ.EditValue.ToString() != "")
            {
                  tempDVCQ  = lke_dsDVCQ.EditValue.ToString();
            }

            string tempLoaiDV="";
            if (lke_loaiDV.EditValue.ToString() != "")
            {
                 tempLoaiDV= lke_loaiDV.EditValue.ToString();
            }

            btn_Huy.Enabled = false;
            btn_Them.Enabled = true;
                       
                QLNSServiceReference.DANHMUC_DONVI dmdv = new QLNSServiceReference.DANHMUC_DONVI();
                dmdv.Ma_DV = tempMaDMDV;
                dmdv.TenDVTT = temptenDMDV;
                dmdv.Ma_DV_CQ = tempDVCQ;
                dmdv.Ma_Loai_DV = tempLoaiDV;
                if (demsua == 1)
                {
                    DialogResult dr = MessageBox.Show("Bạn có chắc muốn cập nhật thông tin?", "CẢNH BÁO", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                    if (dr == DialogResult.OK)
                    {
                        sv.updateDMDV(dmdv);
                        LoadData();
                        demsua = 0;
                        MessageBox.Show("Cập nhật thông tin đơn vị '"+txt_tenDMDV.Text+"' mã '"+txt_maDMDV.Text+"' thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    int kq = sv.addDMDV(dmdv);
                    if (kq != -1)
                    {
                        LoadData();
                        MessageBox.Show("Thêm thông tin ngạch '"+txt_tenDMDV.Text+"' mã '"+txt_maDMDV.Text+"' thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    
                }
           
            
            
        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            btn_Them.Enabled = true;
            btn_Luu.Enabled = false;
            
            DialogResult dr = MessageBox.Show("Bạn có chắc muốn xóa dữ liệu này?!", "CẢNH BÁO", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                sv.deleteDMDV(madv);
                LoadData();
                MessageBox.Show("Xóa thành công!");
            }
        }


        int demsua = 0;
        private void btn_Sua_Click(object sender, EventArgs e)
        {
            if (gridView1.DataRowCount > 0)
            {
                demsua = 1;

                btn_Luu.Enabled = true;
                btn_Them.Enabled = false;
                btn_Huy.Enabled = true;
                btn_Sua.Enabled = false;

                txt_tenDMDV.Enabled = true;
                lke_dsDVCQ.Enabled = true;
                lke_loaiDV.Enabled = true;
            }
            else
            {
                MessageBox.Show("Không có dữ liệu để sửa!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }


        string madv;
        private void gv_dsDMDV_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = false;
            btn_Xoa.Enabled = true;
            btn_Sua.Enabled = true;
            btn_Huy.Enabled = false;
            btn_Them.Enabled = true;

            txt_tenDMDV.Enabled = false;
            txt_maDMDV.Enabled = false;
            lke_loaiDV.Enabled = false;
            lke_dsDVCQ.Enabled = false;

            
            madv = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[0]);

            try
            {
                txt_maDMDV.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[0]);
                txt_tenDMDV.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[1]);
                lke_dsDVCQ.EditValue = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[2]);
                lke_loaiDV.EditValue = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[3]);
                
            }
            catch
            { }
        }

        private void txt_maDMDV_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void groupControl1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            demsua = 0;
            
            btn_Luu.Enabled = false;
            btn_Huy.Enabled = false;
            btn_Them.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;

            txt_tenDMDV.Enabled = false;
            txt_maDMDV.Enabled = false;
            lke_loaiDV.Enabled = false;
            lke_dsDVCQ.Enabled = false;
        }
    }
}
